using System;
using System.IO;
using System.Runtime.InteropServices;
using Egeye.AddIn.Core;
using Egeye.AddIn.Services;
using Egeye.AddIn.Services.Impl;

namespace Egeye.AddIn.Core
{
	/// <summary>
	/// Egeye entry point. Is a singleton.
	/// </summary>
	public class Launcher
	{
        private bool doNotLaunch = false;

        [DllImport("kernel32")]
        static extern bool AllocConsole();

        public Launcher()
		{

		}

        public static void Main(string[] args)
        {
            new Launcher().Run(args);
        }

        public void Run(string[] args)
        {
            TracesProvider.Initialize(new DefaultTrace());

            ShellSetup setup = new ShellSetup();
            setup.ConsoleArgs = args;

            Shell.InitializeOSGi(setup);
            Shell osgi = Shell.Current;

            ParseArgs(args);

            if (!doNotLaunch)
            {
                osgi.Launch();
            }
        }

        public static void Shutdown(Framework framework, int exitcode)
        {
            TracesProvider.TracesOutput.OutputTrace("Egeye is stopping...\n");
            framework.Shutdown();
        }

        protected void ParseArgs(string[] args)
        {
            int startOffset = 0;

            for (int i = startOffset; i < args.Length; i++)
            {
                try
                {
                    if ("-init".Equals(args[i]))
                    {
                        // This is done in an earlier pass, otherwise we
                        // shoot the FW in the foot
                    }
                    else if ("-initlevel".Equals(args[i]))
                    {
                        
                    }
                    else if ("-startlevel".Equals(args[i]))
                    {
                        
                    }
                    else if ("-console".Equals(args[i]))
                    {
                        
                    }
                    else
                    {
                        //error("Unknown option: " + args[i] +
                        //      "\nUse option -help to see all options");
                        //Console.WriteLine("Unknown option: " + args[i] +
                        //      "\nUse option -help to see all options");
                    }
                }
                catch (BundleException ex)
                {
                    
                }
                catch (Exception ex)
                {
                    
                }
            }
        }
    }
}
